var app = getApp()
Page({
  data: {
    pinyin:new Object(),
    car: '',
    result: '',
    val: "",
    show: false
  },
  onLoad: function () {
    this.getKey()
  },

  getKey() {
    let that = this
    wx.cloud.callFunction({
      name: 'getKey',
      data: {
        name: 'carOBD'
      }
    }).then(res => {
      console.log(res.result.data[0].key)
      that.setData({
        car: res.result.data[0].key
      })
    })
  },

  /**
   * 获取用户输入 
   */
  val: function (e) {
    this.setData({
      val: e.detail.value
    })
  },
  // 事件处理函数
  onInput: function (e) {
    var that = this;
      wx.request({
        url: "https://apis.juhe.cn/obdcode/query?key="+that.data.car+"&code="+that.data.val,
        data: {
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          wx.hideLoading();
          if (res.statusCode == 200) {
            that.setData({
              result: res.data.result,
              show: true
            })
          } else {
            that.setData({
              result: '服务器错误！',
              show: false
            })
          }
        },
        fail(res){
          console.log(res)
        }
      })
  },

})
